package com.atlin.project.service.impl;

import com.atlin.project.contant.CommonConstant;
import com.atlin.project.mapper.cds.ClientCodeMapper;
import com.atlin.project.mapper.cds.ClientFileMapper;
import com.atlin.project.model.domain.cds.ClientCode;
import com.atlin.project.model.dto.clientCode.ClientCodeQueryRequest;
import com.atlin.project.model.vo.cds.CdsSyncCheckVo;
import com.atlin.project.model.vo.cds.ClientCodeVO;
import com.atlin.project.service.ClientCodeService;
import com.atlin.project.service.ClientFileService;
import com.atlin.project.utils.sql.SqlUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author atlin
 * @description clientCode
 * @createDate date
 */
@Service
public class ClientCodeServiceImpl extends ServiceImpl<ClientCodeMapper, ClientCode>
        implements ClientCodeService {

    @Resource
    private ClientFileMapper clientFileMapper;

    private ClientFileService clientFileService;

    @Override
    public void valid(ClientCode clientCode, boolean add) {

    }

    @Override
    public ClientCodeVO getClientCodeVO(ClientCode clientCode, HttpServletRequest request) {
        ClientCodeVO clientCodeVO = ClientCodeVO.objToVo(clientCode);
        // todo other operation
        return clientCodeVO;
    }

    /**
     * 获取查询包装类（用户根据哪些字段查询，根据前端传来的请求对象，得到 mybatis 框架支持的查询 QueryWrapper 类）
     *
     * @param clientCodeQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<ClientCode> getQueryWrapper(ClientCodeQueryRequest clientCodeQueryRequest) {
        QueryWrapper<ClientCode> queryWrapper = new QueryWrapper<>();
        if (clientCodeQueryRequest == null) {
            return queryWrapper;
        }
        Long id = clientCodeQueryRequest.getId();
//        String title = clientCodeQueryRequest.getTitle();
//        String content = clientCodeQueryRequest.getContent();
        String sortField = clientCodeQueryRequest.getSortField();
        String sortOrder = clientCodeQueryRequest.getSortOrder();

//        queryWrapper.like(StringUtils.isNotBlank(title), "title", title);
//        queryWrapper.like(StringUtils.isNotBlank(content), "content", content);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq("isDelete", false);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);

        // todo other operation
        return queryWrapper;
    }

    @Override
    public Page<ClientCodeVO> getClientCodeVOPage(Page<ClientCode> clientCodePage, HttpServletRequest request) {
        List<ClientCode> clientCodeList = clientCodePage.getRecords();
        Page<ClientCodeVO> clientCodeVOPage = new Page<>(clientCodePage.getCurrent(), clientCodePage.getSize(), clientCodePage.getTotal());
        if (CollectionUtils.isEmpty(clientCodeList)) {
            return clientCodeVOPage;
        }
        // todo other operation

        // 填充信息
        List<ClientCodeVO> clientCodeVOList = clientCodeList.stream().map(clientCode -> {
            ClientCodeVO clientCodeVO = ClientCodeVO.objToVo(clientCode);
            // todo other operation

            return clientCodeVO;
        }).collect(Collectors.toList());
        clientCodeVOPage.setRecords(clientCodeVOList);
        return clientCodeVOPage;
    }

    /**
     * 客户端文件同步校验
     *
     * @param clientCode 客户端标识码
     * @return CdsSyncCheckVo
     */
    @Override
    public List<CdsSyncCheckVo> syncCheck(String clientCode) {
        List<CdsSyncCheckVo> cdsSyncCheckVos = baseMapper.queryClientFileInfo(new LambdaQueryWrapper<>());

        return cdsSyncCheckVos;
    }
}




